<template>
  <div class="app-container">


    <!-- vxe检索数据组件 -->
    <search-form
      ref="searchForm"
      :formSearchData.sync="formSearchData"
      @searchEventEmit="searchEventEmit"
      @resetSearchEventEmit="resetSearchEventEmit"
    >
    </search-form>


    <!-- 表格组件 -->
    <ex-base-table
      ref="exBaseTable"
      :tableColumn="tableColumn"
      :tableData="tableData"
      :loading.sync="loading"
      :isVxeColSeq="false"
      :isVxeToolbar="true"
      :isVxeCheckbox="true"
      :isVxeHandleS="true"
      @handleRemoveEmit="handleRemoveEmit"
      @handleCreateEmit="handleCreateEmit"
      @handleUpdateEmit="handleUpdateEmit"
      @handleRmoveColEmit="handleRmoveColEmit"
      @customSortMethodEmit="customSortMethodEmit"
    >


    </ex-base-table>

    <!-- 分页组件 -->
    <Pagination
      v-show="tablePage.totalResult>0"
      :total="tablePage.totalResult"
      :page.sync="tablePage.currentPage"
      :limit.sync="tablePage.pageSize"
      @pagination="pageFindList"
    />


    <!-- 表单提价组件 -->
    <submit-form
      ref="submitForm"
      :dialogFormVisible.sync="dialogFormVisible"
      :dialogStatus="dialogStatus"
      :formSubmitData.sync="formSubmitData"
      :bCCurveType="bCCurveType"
      :bCCurve="bCCurve"
      :adjTypeOptions="adjTypeOptions"
      :adjCpOptions="adjCpOptions"
      :sCvTypeOptions="sCvTypeOptions"
      :sCvCodeOptions="sCvCodeOptions"
      :mTermCodeOptions="mTermCodeOptions"
      :sTermCodeOptions="sTermCodeOptions"
      @handleFromUpdateDataEmit="handleFromUpdateDataEmit"
      @handleFromCreateDataEmit="handleFromCreateDataEmit"
      @resetFormDataEmit="resetFormSubmitData"
      @mCvTypeChangeEmit="mCvTypeChangeEmit"
      @sCvTypeChangeEmit="sCvTypeChangeEmit"
      @mCvCodeChangeEmit="mCvCodeChangeEmit"
      @sTermCodeChangeEmit="sTermCodeChangeEmit"
    >
    </submit-form>

    <!-- 曲线图 -->
    <el-dialog
      :title="chartTitle"
      :visible.sync="DialogChartVisible"
      append-to-body
    >
      <ExManyLineChart
        :chart-data="lineChartData"
      />
    </el-dialog>

  </div>
</template>

<script>
import XEUtils from 'xe-utils'
import * as _ from 'lodash'
// 基础折线图图表
import ExManyLineChart from '@/components/Echarts/LineChart/ExManyLineChart'

import {
  fetchDifferenceValue,  /* TODO: 暂时测试获取列表接口 */
  addDifferenceValue,
  updateDifferenceValue,
  deleteDifferenceValue,
  bCArithmeticMeanQueryCurveType,
  bCArithmeticMeanQueryCurve,
  bCArithmeticMeanQueryADJtype,
  bCArithmeticMeanQueryADJcp,
  bCArithmeticMeanQueryTerm
} from './service/baseApi'
// 基础表格组件
import ExBaseTable from './components/ExBaseTable'
// 分页
import Pagination from '@/components/Pagination'
// 文章表单
import SubmitForm from './components/SubmitForm'
// 检索表单
import SearchForm from './components/SearchForm'
import resetFormSubmitData from '@/views/mixins/resetFormSubmitData'

export default {
  name: 'vxe-basetable',
  mixins:[resetFormSubmitData],
  components: {
    //PageHeaderLayout,
    ExBaseTable,
    Pagination,
    SubmitForm,
    SearchForm,
    ExManyLineChart,
  },
  props: {
    /* 当前行数据 */
    currentRow: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  // 定义属性
  data() {
    return {
      /* 检索相关 */
      // 检索条件
      formSearchData: {},
      // 原曲线类型
      bCCurveType: [],
      // 原曲线
      bCCurve: [],
      // 调整类型选项
      adjTypeOptions: [],
      // 调整值选项
      adjCpOptions: [],
      // 相减类型
      sCvTypeOptions: [],
      // 相减曲线
      sCvCodeOptions: [],
      // 原曲线期限点
      mTermCodeOptions: [],
      // 相减曲线期限点
      sTermCodeOptions: [],

      // 排除条件
      sortField: '',
      sortFlag: '',

      /* 表单相关 */
      dialogFormVisible: false, // 修改删除弹出框
      dialogStatus: '',
      // 表单数据对象
      formSubmitData: {
        id: '',
        m_cv_type: '',
        m_cv_code: '',
        s_cv_type: '',
        s_cv_code: '',
        m_term_code: '',
        s_term_code: '',
        adj_type: '',
        adj_value: '',
        adj_cp: '',
        remark: ''
      },
      initFormSubmitData: {
        id: '',
        m_cv_type: '',
        m_cv_code: '',
        s_cv_type: '',
        s_cv_code: '',
        m_term_code: '',
        s_term_code: '',
        adj_type: '',
        adj_value: '',
        adj_cp: '',
        remark: ''
      },
      /* 分页相关 */
      tablePage: {
        currentPage: 1, //当前页
        pageSize: 10,  // 每一页页数
        totalResult: 0
      },
      /* 表格相关 */
      tableColumn: [], // 表格字段
      loading: false, //表格loading特效
      tableData: [],  //表格数据

      /* 子表相关 */
      DialogSonTableVisible: false,


      /* 是否是更新DOM初始化操作 */
      isUpdateDOMInit: false,

      /* 曲线图 */
      chartTitle: '',
      DialogChartVisible: false,
      lineChartData: {
        tooltipFormatter: '',
        legendData: [],
        xAxisData: [],
        seriesData: []
      },
      lineChartColors: [
        '#5470c6',
        '#91cc75',
        '#fac858',
        '#ee6666',
        '#73c0de',
        '#3ba272',
        '#fc8452',
        '#9a60b4',
        '#ea7ccc'
      ],
    } //datareturn-end

  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    // 计算属性作为中间 使用watch层监听对象某个属性
    // 生效时间
    formSubmitDataSdate() {
      return this.formSubmitData.s_date
    },
    // 期限方案id
    formSubmitDataTpId() {
      return this.formSubmitData.tp_id
    },
  },
  // 监控data中的数据变化
  watch: {
    currentRow() {
      this.findList()
    },
    // formSubmitDataSdate(newValue, oldValue) {
    //   this.debouncedGetDeadlineSetList()
    // },
    // formSubmitDataTpId(newValue, oldValue) {
    //   this.debouncedGetDeadlineSetList()
    // },

  },
  // 方法集合
  methods: {


    /* API数据操作相关 */
    // 获取原曲线类型/相减曲线类型
    getBCArithmeticMeanQueryCurveType() {
      bCArithmeticMeanQueryCurveType({}).then(({data}) => {
        this.bCCurveType = data
        this.sCvTypeOptions = data
      }, error => {
        // alert(error);
      })
    },
    // 获取原曲线
    mCvTypeChangeEmit(val) {
      console.log('获取原曲线');
      this.getBCArithmeticMeanQueryCurve(val, 'y')
    },
    // 获取相减曲线
    sCvTypeChangeEmit(val) {
      console.log('获取相减曲线');
      this.getBCArithmeticMeanQueryCurve(val, 'xj')
    },
    // 获取曲线
    getBCArithmeticMeanQueryCurve(val, type) {
      bCArithmeticMeanQueryCurve({
        m_cv_type: val
      }).then(({data}) => {
        if (type == 'y') {
          this.bCCurve = data
          this.formSubmitData.m_cv_code = ''
          this.formSubmitData.m_term_code = ''
        }
        if (type == 'xj') {
          this.sCvCodeOptions = data
          this.formSubmitData.s_cv_code = ''
          this.formSubmitData.s_term_code = ''
        }

      }, error => {
        // alert(error);
      })
    },




    // 原曲线期限点
    mCvCodeChangeEmit(val) {
      this.formSubmitData.m_term_code = ''
      bCArithmeticMeanQueryTerm({
        type: this.formSubmitData.m_cv_type,
        cv_code: this.formSubmitData.m_cv_code,
        s_date: this.formSubmitData.s_date,
        e_date: this.formSubmitData.e_date
      }).then(({data}) => {
        this.mTermCodeOptions = data

      }, error => {
        // alert(error);
      })
    },
    // 相减曲线期限点
    sTermCodeChangeEmit(val) {
      this.formSubmitData.s_term_code = ''
      bCArithmeticMeanQueryTerm({
        type: this.formSubmitData.s_cv_type,
        cv_code: this.formSubmitData.s_cv_code,
        s_date: this.formSubmitData.s_date,
        e_date: this.formSubmitData.e_date
      }).then(({data}) => {
        this.sTermCodeOptions = data

      }, error => {
        // alert(error);
      })
    },

    // 获取调整类型选项
    getBCArithmeticMeanQueryADJtype() {
      bCArithmeticMeanQueryADJtype({}).then(({data}) => {
        this.adjTypeOptions = data
      }, error => {
        // alert(error);
      })
    },
    // 调整单位选项
    getBCArithmeticMeanQueryADJcp() {
      bCArithmeticMeanQueryADJcp().then(({data}) => {
        this.adjCpOptions = data
      }, error => {
        // alert(error);
      })
    },


    // // 获取期限设置数据
    // getDeadlineSetList() {

    //   if (this.isUpdateDOMInit === false) {
    //     // 初始化-曲线期限设置参数
    //     this.curveDeadlineSetList = []
    //     // 初始化-删除开头为"term_"的元素
    //     XEUtils.objectEach(this.formSubmitData, (item, key) => {
    //       if (key.startsWith("term_") && key !== 'term_rate') {
    //         delete this.formSubmitData[`${key}`];
    //       }
    //     })

    //     curveTermQueryTermPrice({
    //       m_id: this.formSubmitData.tp_id,
    //       // m_id: '8fa50871db694d688f0837c87ff879c1', /* TODO: 测试期限id */
    //       s_date: this.formSubmitData.s_date,
    //       // s_date: '2022-08-01', /* TODO: 测试生效日期 */
    //       // e_date: this.formSubmitData.e_date
    //     }).then(({data}) => {
    //       this.curveDeadlineSetList = data
    //       // 初始化-增加开头为"term_"的元素
    //       // formSubmitDataCom[`term_${item.term_code}`]
    //       XEUtils.objectEach(data, (item, key) => {
    //         this.$set(this.formSubmitData, `term_${item.term_code}`, 0)
    //         // 一下写法不是响应式！！！不要用
    //         // this.formSubmitData[`term_${item.term_code}`] = item.rate
    //       })

    //     }, error => {

    //     })
    //   }
    //   // 重置-是否是更新DOM初始化操作
    //   this.isUpdateDOMInit = false

    // },
    // 查询数据
    findList() {
      this.loading = true
      //请求表格数据
      fetchDifferenceValue({
        m_id: this.currentRow.id,
        m_cv_name: this.currentRow.m_cv_name,
        s_cv_name: this.currentRow.s_cv_name,
        ...this.formSearchData,  //检索条件
      }).then(({data}) => {
        // 给表格数据赋值
        this.tableData = data.rows
        // 给分页总数赋值
        this.tablePage.totalResult = data.total
        // 关闭加载特效
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      })
    },

    pageFindList() {

      this.findList()
    },
    /* 表格组件相关 */
    // 初始化表格字段
    initTableCols() {
      const tabCols = [  //动态增加的字段 传进来的
        {field: 'bs_cv_name', title: '基础曲线', width: 300},
        {field: 's_date', title: '生效日期', width: 200},
        {field: 'e_date', title: '失效日期', width: 200},
      ]
      this.tableColumn = tabCols;
    },
    // 表格工具组-新增表单弹窗（需要表单组件）
    handleCreateEmit(type) {
      // 重置表单内容
      this.resetFormSubmitData({
        replaceObj: {
          bs_cv_code: this.currentRow.cv_name,
          s_date: this.currentRow.s_date,
          e_date: this.currentRow.e_date,
          // curr_name: this.currentRow.curr_name, //币种
          // cv_type: this.currentRow.cv_typename,
          // e_date: '9999-12-31', //失效日期
        },
        exclude: [
          // 'curr_code'
        ]
      })
      this.dialogStatus = type
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.submitForm.$refs['dataForm'].clearValidate()
      })
    },
    // 行内按钮-修改数据弹窗（需要表单组件）
    handleUpdateEmit(row, type) {
      this.isUpdateDOMInit = true
      this.dialogStatus = type
      this.dialogFormVisible = true
      // 重置表单内容
      this.resetFormSubmitData({
        resetObj: Object.assign({}, row),
        // 重置数据值
        replaceObj: {
          // cv_type: this.currentRow.cv_typename,
        },
        // 排除不重置元素
        exclude: []
      })

      // 初试化-增加开头为"term_"的元素 formSubmitDataCom[`term_${item.term_code}`]
      // XEUtils.objectEach(row.term_rate, (item, key) => {
      //   this.$set(this.formSubmitData, `term_${item.term_code}`, item.rate)
      //   // 一下写法不是响应式！！！不要用
      //   // this.formSubmitData[`term_${item.term_code}`] = item.rate
      // })
      // this.curveDeadlineSetList = row.term_rate
      this.$nextTick(() => {
        this.$refs.submitForm.$refs['dataForm'].clearValidate()
      })
    },
    // 单行删除
    handleRmoveColEmit(row) {
      // this.$refs.exBaseTable.$refs.xTable.remove(row)
      deleteDifferenceValue({
        id: row.id,
        m_id: this.currentRow.id,
      }).then(({data}) => {

        this.findList()

      }, error => {
        // alert(error);
      })
    },

    /* 表单组件相关 */
    // 更新
    handleFromUpdateDataEmit() {
      updateDifferenceValue({
        m_id: this.currentRow.id,
        ...this.formSubmitData,  //检索条件
      }).then(({data}) => {
        this.findList()
      }, error => {
        // alert(error);
      })
    },

    // 增加
    handleFromCreateDataEmit() {
      addDifferenceValue({
        m_id: this.currentRow.id,
        ...this.formSubmitData,  //检索条件
      }).then(({data}) => {

        this.findList()
        // 关闭加载特效

      }, error => {
        // alert(error);
      })
    },

    /* 检索相关 */
    // 检索
    searchEventEmit() {
      this.tablePage.currentPage = 1
      this.findList()
    },
    // 重置
    resetSearchEventEmit() {
      // 重置代码...
      this.tablePage.currentPage = 1
      this.findList()
    },
    /* 排序 */
    customSortMethodEmit(property, order) {
      this.sortField = property
      this.sortFlag = order
      this.findList()

    },
    handleRemoveEmit() {
    },

    /* 子表格弹出 */
    viewSonTable(row) {
      this.DialogSonTableVisible = true
    },

  }, //method-end
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    // 初始化表格字段
    this.initTableCols()
    // 初始化表格数据
    this.findList()

    // 获取曲线类型
    this.getBCArithmeticMeanQueryCurveType()

    // 获取调整类型
    this.getBCArithmeticMeanQueryADJtype()
    // 获取调整单位
    this.getBCArithmeticMeanQueryADJcp()


  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>


</style>